package com.woniu.humanresource.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.humanresource.entity.AssetsCategory;
import com.woniu.humanresource.service.AssetsCategoryService;
import com.woniu.humanresource.dao.AssetsCategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author xiang
* @description 针对表【assetscategory】的数据库操作Service实现
* @createDate 2022-07-10 17:50:54
*/
@Service
public class AssetsCategoryServiceImpl extends ServiceImpl<AssetsCategoryMapper, AssetsCategory>
    implements AssetsCategoryService{

    @Autowired
    AssetsCategoryMapper assetsCategoryMapper;

    //获取所有资产分类分类
    @Override
    public List<AssetsCategory> getAllAssetsCategory() {
        List<AssetsCategory> assetsCategories = assetsCategoryMapper.selectAllAssetsCategory();
        return assetsCategories;
    }

    //添加根节点
    @Override
    public void addRootNodeAssetsCategory(String newAssetsCategoryName) {
        AssetsCategory newAssetsCategory=new AssetsCategory();
        newAssetsCategory.setName(newAssetsCategoryName);
        newAssetsCategory.setParentid(0);

        assetsCategoryMapper.insert(newAssetsCategory);
    }

    //添加子节点分类
    @Override
    public void addAssetsCategory(String newAssetsCategoryName, AssetsCategory parentAssetsCategory) {
        AssetsCategory newAssetsCategory=new AssetsCategory();
        newAssetsCategory.setName(newAssetsCategoryName);//用户输入的名字
        newAssetsCategory.setParentid(parentAssetsCategory.getId());//父节点的id

        assetsCategoryMapper.insert(newAssetsCategory);
    }

    //删除勾选的节点
    @Override
    public void removeAssetsCategory(List<AssetsCategory> assetsCategories) {
        for(AssetsCategory assetsCategory:assetsCategories){
            assetsCategoryMapper.deleteById(assetsCategory.getId());
        }
    }

    //编辑
    @Override
    public void editAssetsCategory(String newAssetsCategoryName,AssetsCategory assetsCategory) {
        AssetsCategory editAssetsCategory=new AssetsCategory();
        editAssetsCategory.setId(assetsCategory.getId());
        editAssetsCategory.setName(newAssetsCategoryName);

        assetsCategoryMapper.updateById(editAssetsCategory);
    }
}




